package com.example.administrator.hqq.adapter.discover;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.adapter.SetOnClickItem;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @name: HQQ
 * @describe:
 * @author: 康海涛 QQ2541849981
 * @time: 2018/11/25 19:51
 * @remark:
 */
public class ReleasePostAdapter extends BaseAdapter {
    private Context context;
    private List<String> list;
    private SetOnClickItem setOnClickItem;
    private int num = 6;
    private DeletePic deletePic;

    public void setDeletePic(DeletePic deletePic) {
        this.deletePic = deletePic;
    }

    public ReleasePostAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    public void setNum(int num) {
        this.num = num;
    }

    @Override
    public int getCount() {
        if (list.size() == 0) {
            return 1;
        }
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        view = LayoutInflater.from(context).inflate(R.layout.release_forum_grid_item, viewGroup, false);
        viewHolder = new ViewHolder(view);
        Log.e("kang", "getView: 显示图片");
        if (list.size() != 0) {
            Glide.with(context).load(list.get(i)).fitCenter().into(viewHolder.postImage);
            viewHolder.deletePic.setVisibility(View.VISIBLE);
        } else {
            viewHolder.deletePic.setVisibility(View.GONE);
        }
        viewHolder.deletePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (deletePic!=null)
                {
                    deletePic.delete(i);
                }
            }
        });
        return view;
    }

    public void setSetOnClickItem(SetOnClickItem setOnClickItem) {
        this.setOnClickItem = setOnClickItem;
    }

    public interface DeletePic{
        void delete(int position);
    }
    static class ViewHolder {
        @BindView(R.id.post_image)
        ImageView postImage;
        @BindView(R.id.delete_pic)
        ImageView deletePic;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
